/* eslint-disable no-extra-parens */
import { InitModalMethod, ParseModalMethod } from './defination';

/**
 * 设置表单属性
 * @param { Object } form 表单数据
 * @param { Object } workstep cached的ai工作流
 */
export const initModalData = (form = {}, workstep = {}, tts = false) => {
	if (!form || !workstep || !workstep.do) {
		return;
	}

	form.aiSpeak = workstep.do?.data; // 1
	form.aiSpeakVideo = workstep.do?.video;

	form.cardCollectionType = workstep.cardType;
	form.openOCR = workstep.openOCR;

	/**
	 *  {
            "name":"身份证号",
            "fieldName":"idNumber",//身份证号
            "parameterName":"{{客户信息_访客_客户ID_1376}}",// 参数名 绑定的变量
        },
	 */
	form.identifyResult = workstep.identifyResult || [];

	form.confidence = workstep.after.data?.confidence; // 2

	form.waitTime = workstep.timeout || 0;

	// yes
	if (workstep.answer[0]?.after) {
		form.wordsYes = workstep.answer[0]?.do?.data; // 3
		form.wordsYesVideo = workstep.answer[0]?.do?.video;
		form.nextStepYes = workstep.answer[0]?.after?.id;
	} else {
		form.nextStepYes = workstep.answer[0]?.do?.id;
		form.wordsYesVideo = workstep.answer[0]?.do?.video;
	}
	// no
	if (workstep.answer[1]?.after) {
		form.wordsNo = workstep.answer[1]?.do?.data; // 4
		form.wordsNoVideo = workstep.answer[1]?.do?.video;
		form.nextStepNo = workstep.answer[1]?.after?.id;
	} else {
		form.nextStepNo = workstep.answer[1]?.do?.id;
		form.wordsNoVideo = workstep.answer[1]?.do?.video;
	}
	// other
	if (workstep.answer[2]?.after) {
		form.wordsOther = workstep.answer[2]?.do?.data; // 5
		form.wordsOtherVideo = workstep.answer[2]?.do?.video;
	} else if (tts) {
		form.wordsOtherVideo = workstep.answer[2]?.do?.video;
	}
	if (workstep.errorRemind?.after) {
		form.wordsEnd = workstep.errorRemind?.do?.data; // 6
		form.wordsEndVideo = workstep.errorRemind?.do?.video;
		form.nextStepOther = workstep.errorRemind?.after?.id;
	} else {
		form.nextStepOther = workstep.errorRemind?.do?.id;
		form.wordsEndVideo = workstep.errorRemind?.do?.video;
	}
	form.otherTimesMax = workstep.errorRemind?.maximum; // 5
	form.wordsOverTime = workstep.errorRemind?.timeout; // 等待超时

	// result failed
	if (workstep.answer[3]?.do) {
		form.resultFailedSpeak = workstep.answer[3]?.do?.data; // 5
		form.resultFailedSpeakVideo = workstep.answer[3]?.do?.video;
	} else if (tts) {
		form.resultFailedSpeakVideo = workstep.answer[3]?.do?.video;
	}
	if (workstep.identifyError?.after) {
		form.resultFailedNextStepSpeak = workstep.identifyError?.do?.data; // 6
		form.resultFailedNextStepSpeakVideo = workstep.identifyError?.do?.video;
		form.resultFailedNextStep = workstep.identifyError?.after?.id;
	} else {
		form.resultFailedNextStep = workstep.identifyError?.do?.id;
		form.resultFailedNextStepSpeakVideo = workstep.identifyError?.do?.video;
	}
	form.resultFailedTimesMax = workstep.identifyError?.maximum; // 5

	/** 变量存储处理开始 */
	form['aiSpeakVariableId'] = workstep.do.variableId;
	form['aiSpeakVariables'] = workstep.do.variables?.filter(item => form['aiSpeak']?.indexOf(`{{${item.name}}}`) !== -1);
	form['wordsYesVariableId'] = workstep.answer[0].do.variableId;
	form['wordsYesVariables'] = workstep.answer[0].do.variables?.filter(item => form['wordsYes']?.indexOf(`{{${item.name}}}`) !== -1);
	form['wordsNoVariableId'] = workstep.answer[1].do.variableId;
	form['wordsNoVariables'] = workstep.answer[1].do.variables?.filter(item => form['wordsNo']?.indexOf(`{{${item.name}}}`) !== -1);
	form['wordsOtherVariableId'] = workstep.answer[2].do.variableId;
	form['wordsOtherVariables'] = workstep.answer[2].do.variables?.filter(item => form['wordsOther']?.indexOf(`{{${item.name}}}`) !== -1);
	form['wordsEndVariableId'] = workstep.errorRemind.do.variableId;
	form['wordsEndVariables'] = workstep.errorRemind.do.variables?.filter(item => form['wordsEnd']?.indexOf(`{{${item.name}}}`) !== -1);

	form['resultFailedSpeakVariables'] = workstep.answer[3].do.variables?.filter(item => form['resultFailedSpeak']?.indexOf(`{{${item.name}}}`) !== -1);
	form['resultFailedNextStepSpeakVariables'] = workstep.identifyError.do.variables?.filter(item => form['resultFailedNextStepSpeak']?.indexOf(`{{${item.name}}}`) !== -1);
	/** 变量存储处理结束 */

	form.name = workstep.name;
};
InitModalMethod['cardCollection'] = initModalData;

/**
 * 解析表单属性
 * @param { Object } form 表单数据
 * @param { Object } workstep cached的ai工作流
 */
export const parseModalData = (form = {}, workstep = {}, tts = false) => {
	console.log(form, workstep);
	if (!form || !workstep || !workstep.do) {
		return;
	}

	if (!form.openOCR) {
		form.resultFailedSpeak = '';
		form.resultFailedSpeakVideo = null;
		form.resultFailedNextStepSpeakVideo = null;
		form.resultFailedSpeakVariables = [];
		form.resultFailedNextStepSpeakVariables = [];
		form.resultFailedNextStep = null; // 结果失败下一步
		form.resultFailedWaitTimes = null; // 识别失败等待时长
		form.resultFailedTimesMax = null; //  结果失败最大允许次数
		form.resultFailedSpeak = null; // 结果失败 AI 说
		form.resultFailedNextStepSpeak = null; // 结果失败 AI 说
		form.identifyResult = [];
	}
	workstep.do.data = form.aiSpeak; // 1
	workstep.do.video = form.aiSpeakVideo;
	workstep.cardType = form.cardCollectionType;
	workstep.openOCR = form.openOCR;
	workstep.identifyResult = form.identifyResult || [];
	// confidence
	workstep.timeout = form.waitTime;
	workstep.after.data.confidence = form.confidence; // 2

	// yes
	if ((form.wordsYes && !tts) || (form.wordsYesVideo && !tts) || (tts && form.wordsYes && form.wordsYesVideo)) {
		workstep.answer[0].do.data = form.wordsYes; // 3
		workstep.answer[0].do.video = form.wordsYesVideo;
		workstep.answer[0].after.id = form.nextStepYes;
		workstep.answer[0].after.no = form.nextStepYes;
	} else {
		workstep.answer[0].do = Object.assign({}, workstep.answer[0].after);
		workstep.answer[0].do.id = form.nextStepYes;
		workstep.answer[0].do.no = form.nextStepYes;
		workstep.answer[0].do.video = form.wordsYesVideo;
		delete workstep.answer[0].after;
	}

	// no
	if ((form.wordsNo && !tts) || (form.wordsNoVideo && !tts) || (tts && form.wordsNo && form.wordsNoVideo)) {
		workstep.answer[1].do.data = form.wordsNo; // 4
		workstep.answer[1].do.video = form.wordsNoVideo;
		workstep.answer[1].after.id = form.nextStepNo;
		workstep.answer[1].after.no = form.nextStepNo;
	} else {
		workstep.answer[1].do = Object.assign({}, workstep.answer[1].after);
		workstep.answer[1].do.id = form.nextStepNo;
		workstep.answer[1].do.no = form.nextStepNo;
		workstep.answer[1].do.video = form.wordsNoVideo;
		delete workstep.answer[1].after;
	}

	// other
	if ((form.wordsOther && !tts) || (form.wordsOtherVideo && !tts) || (tts && form.wordsOther && form.wordsOtherVideo)) {
		workstep.answer[2].do.data = form.wordsOther; // 5
		workstep.answer[2].do.video = form.wordsOtherVideo;
	} else {
		workstep.answer[2].do = Object.assign({}, workstep.answer[2].after);
		workstep.answer[2].do.video = form.wordsOtherVideo;
		delete workstep.answer[2].after;
	}
	if ((form.wordsEnd && !tts) || (form.wordsEndVideo && !tts) || (tts && form.wordsEnd && form.wordsEndVideo)) {
		workstep.errorRemind.after.id = form.nextStepOther;
		workstep.errorRemind.after.no = form.nextStepOther;
		workstep.errorRemind.do.data = form.wordsEnd; // 7
		workstep.errorRemind.do.video = form.wordsEndVideo;
	} else {
		workstep.errorRemind.do = Object.assign({}, workstep.errorRemind.after);
		workstep.errorRemind.do.id = form.nextStepOther;
		workstep.errorRemind.do.no = form.nextStepOther;
		workstep.errorRemind.do.video = form.wordsEndVideo;
		delete workstep.errorRemind.after;
	}

	workstep.errorRemind.maximum = form.otherTimesMax; // 4
	workstep.errorRemind.timeout = form.wordsOverTime; // 等待超时
	// result failed
	if ((form.resultFailedSpeak && !tts) || (form.resultFailedSpeakVideo && !tts) || (tts && form.resultFailedSpeak && form.resultFailedSpeakVideo)) {
		workstep.answer[3].do.data = form.resultFailedSpeak; // 5
		workstep.answer[3].do.video = form.resultFailedSpeakVideo;
	} else {
		workstep.answer[3].do = Object.assign({}, workstep.answer[3].after);
		workstep.answer[3].do.video = form.resultFailedSpeakVideo;
		delete workstep.answer[3].after;
	}
	if ((form.resultFailedNextStepSpeak && !tts) || (form.resultFailedNextStepSpeakVideo && !tts) || (tts && form.resultFailedNextStepSpeak && form.resultFailedNextStepSpeakVideo)) {
		workstep.identifyError.after.id = form.resultFailedNextStep;
		workstep.identifyError.after.no = form.resultFailedNextStep;
		workstep.identifyError.do.data = form.resultFailedNextStepSpeak; // 7
		workstep.identifyError.do.video = form.resultFailedNextStepSpeakVideo;
	} else {
		workstep.identifyError.do = Object.assign({}, workstep.identifyError.after);
		workstep.identifyError.do.id = form.resultFailedNextStep;
		workstep.identifyError.do.no = form.resultFailedNextStep;
		workstep.identifyError.do.video = form.resultFailedNextStepSpeakVideo;
		delete workstep.identifyError.after;
	}

	workstep.identifyError.maximum = form.resultFailedTimesMax; // 4

	/** 变量存储处理开始 */
	workstep.do.variableId = form['aiSpeakVariableId'];
	workstep.do.variables = form['aiSpeakVariables']?.filter(item => form['aiSpeak'].indexOf(`{{${item.name}}}`) !== -1);

	workstep.answer[0].do.variableId = form['wordsYesVariableId'];
	workstep.answer[0].do.variables = form['wordsYesVariables']?.filter(item => form['wordsYes'].indexOf(`{{${item.name}}}`) !== -1);

	workstep.answer[1].do.variableId = form['wordsNoVariableId'];
	workstep.answer[1].do.variables = form['wordsNoVariables']?.filter(item => form['wordsNo'].indexOf(`{{${item.name}}}`) !== -1);

	workstep.answer[2].do.variableId = form['wordsOtherVariableId'];
	workstep.answer[2].do.variables = form['wordsOtherVariables']?.filter(item => form['wordsOther'].indexOf(`{{${item.name}}}`) !== -1);

	workstep.errorRemind.do.variableId = form['wordsEndVariableId'];
	workstep.errorRemind.do.variables = form['wordsEndVariables']?.filter(item => form['wordsEnd'].indexOf(`{{${item.name}}}`) !== -1);

	workstep.answer[3].do.variables = form['resultFailedSpeakVariables']?.filter(item => form['resultFailedSpeak'].indexOf(`{{${item.name}}}`) !== -1);

	workstep.identifyError.do.variables = form['resultFailedNextStepSpeakVariables']?.filter(item => form['resultFailedNextStepSpeak'].indexOf(`{{${item.name}}}`) !== -1);

	/** 变量存储处理结束 */

	workstep.name = form.name;
};
ParseModalMethod['cardCollection'] = parseModalData;
